<template>
  <div>
    <el-container>
      <!-- 左侧导航栏 -->
      <el-aside :style="asideStyle">
        <navbar :menus="menus" :collapse="isCollapse"></navbar>
      </el-aside>
      <!-- 右侧主内容区 -->
      <el-container class="container">
        <!-- 头部 -->
        <el-header>
          <i
            :class="iconDeg"
            class="el-icon-s-fold"
            @click="handleCollapse"
          ></i>

          <el-dropdown>
            <span class="el-dropdown-link">
              用户的名<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>用户信息</el-dropdown-item>
              <el-dropdown-item>修改密码</el-dropdown-item>
              <el-dropdown-item>修改头像</el-dropdown-item>
              <el-dropdown-item @click.native="handleLogout"
                >注销</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
        <!-- 中间内容区 -->
        <el-main><router-view></router-view></el-main>
        <!-- 底部 -->
        <el-footer>
            Copyright © 2021-2021 就爱科技 版权所有. All rights reserved.
          </el-footer>
      </el-container>
    </el-container>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确定要注销吗？请及时保存你的工作^_^</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmLogout">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import routes from "../router/routes";
import Navbar from "./Navbar";
import {clearToken} from '../utils/auth'
export default {
  components: {
    Navbar,
  },
  data() {
    return {
      isCollapse: false,
      dialogVisible: false,
    };
  },
  methods: {
    handleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    handleLogout() {
      console.log("我的注销");
      this.dialogVisible = true;
    },
    handleConfirmLogout() {
      console.log("确定注销");
      clearToken()
      // localStorage.removeItem("accessToken");
      // localStorage.removeItem("refreshToken");
      this.$router.push('/login')
    },
    resolveForMenu(arr, parentPath = "") {
      let newArr = [];
      // 遍历传入的arr数组

      // 处理上级路径

      //判断当前对象（路由）是否具有children节点，如果有，递归处理children节点后，
      //将当前节点加入数组，，如果没有则直接将当前节点加入新数组

      //返回最后处理完成的数组

      arr.forEach((route) => {
        parentPath =
          parentPath && parentPath.lastIndexOf("/") !== parentPath.length - 1
            ? parentPath + "/"
            : parentPath;
        // console.log(parentPath);
        let obj = Object.assign({}, route);
        obj.path = parentPath + obj.path;
        if (obj.children && obj.children.length > 0) {
          obj.children = this.resolveForMenu(obj.children, obj.path);
        }
        // 如果某路由元数据中，设置为当前路由为隐藏，则当前路由将不作为导航菜单显示，只将其children加入到菜单数组
        if (obj.meta.hidden) {
          // 如果存在children属性，那么继续处理下（将所有子节点提升并加入到返回数组），反之不作处理
          if (obj.children && obj.children.length > 0) {
            obj.children.forEach((item) => {
              let tmp = Object.assign({}, item);
              newArr.push(tmp);
            });
          }
        } else {
          newArr.push(obj);
        }
      });

      return newArr;
    },
  },
  computed: {
    menus() {
      var arr = this.resolveForMenu(routes);
      return arr;
    },
    asideStyle() {
      return {
        width: this.isCollapse ? "auto" : "200px",
      };
    },
    iconDeg() {
      return {
        "icon-deg": this.isCollapse ? true : "",
      };
    },
  },
};
</script>

<style scoped>
body {
  background-color: #545c64 !important;
  margin: 0px !important;
}
.el-header {
  background-color: #eeeeee;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.container{
  height: 100vh;
}
.el-footer {
  background-color: #eeeeee;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}
.el-icon-s-fold {
  font-size: 1.5rem;
}
.icon-deg {
  transform: rotate(-90deg);
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>